package com.zh.work.controller.platform.branch;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.dao.platform.branch.BranchBizMapper;
import com.zh.work.entity.branch.BranchBizEntity;
import com.zh.common.entity.*;
import com.zh.work.service.platform.branch.BranchBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 机构业务管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/08/24
 * @Version: 1.0.1
 */
//@PreAuthorize("hasAuthority('/branch_set/brc_biz')")
@Api(value = "机构业务管理控制器", produces = "application/json", tags = {"机构业务管理控制器"})
@RestController
@RequestMapping("/branchBiz")
public class BranchBizController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/08/24
     */
    @Autowired
    private BranchBizMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/08/24
     */
    @Autowired
    private BranchBizService service;


//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/add')")
    @ApiOperation(value = "新增一条记录", tags = {"putAddBranchBiz"}, notes = "新增一条记录")
    @PutMapping("/putAddBranchBiz")
    @ResponseBody
    public JSONResult<Object> putAddBranchBiz(@ApiParam(value = "机构业务实体类", name = "branchBizEntities") @RequestBody List<BranchBizEntity> branchBizEntities) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (this.service.addBiz(branchBizEntities) > 0) {
                // 返回新增主键
//                System.out.println(branchBizEntity.getId());
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/update')")
    @ApiOperation(value = "通过id修改信息", tags = {"putModBranchBizById"}, notes = "通过id修改信息")
    @PutMapping("/putModBranchBizById")
    @ResponseBody
    public JSONResult<Object> putModBranchBizById(@ApiParam(value = "机构业务实体类", name = "branchBizEntity") @RequestBody BranchBizEntity branchBizEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchBizEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/del')")
    @ApiOperation(value = "根据主键删除记录", tags = {"deleteBranchBizById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteBranchBizById")
    public JSONResult<Object> deleteBranchBizById(@ApiParam(value = "机构业务实体类", name = "branchBizEntity") BranchBizEntity branchBizEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchBizEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/dels')")
    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteBranchBizBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteBranchBizBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteBranchBizBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody @RequestParam("ids") List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/get')")
    @ApiOperation(value = "根据id查询详细信息", tags = {"getBranchBizById"}, notes = "根据id查询详细信息")
    @GetMapping("/getBranchBizById")
    public JSONResult<BranchBizEntity> getBranchBizById(@ApiParam(value = "机构业务实体类", name = "branchBizEntity") BranchBizEntity branchBizEntity) {
        JSONResult<BranchBizEntity> jsonResult = new JSONResult<>();
        try {
            BranchBizEntity result = branchBizEntity.selectById(branchBizEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


//    @PreAuthorize("hasAuthority('/branch_set/brc_biz/get')")
    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesBranchBizs"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesBranchBizs")
    public JSONResult<List<BranchBizEntity>> getPagesBranchBizs(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "机构业务实体类", name = "branchBizEntity") BranchBizEntity branchBizEntity) {
        JSONResult<List<BranchBizEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<BranchBizEntity> page = new Page<>(pageNo, pageSize);
            IPage<BranchBizEntity> result = this.mapper.selectPage(page, new LambdaQueryWrapper<BranchBizEntity>()
                    .like(StringUtils.isNotEmpty(branchBizEntity.getBranchNo()), BranchBizEntity::getBranchNo, branchBizEntity.getBranchNo())
                    .eq(Objects.nonNull(branchBizEntity.getBizType()), BranchBizEntity::getBizType, branchBizEntity.getBizType())
            );
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());

//            redisTemplate.opsForValue().set("test", list);
//            List<ParamEntity> list1 = (List<ParamEntity>)redisTemplate.opsForValue().get("test");
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
